<?php
/*
 *      osdefaultlistdictionaryedit.php
 *      
 *      Copyright 2010 Vitaliy Rakitin <virak@ukr.net>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

//require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/ositems.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/ositem.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/DB/DB.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/system.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscomponent.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oslabel.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osbutton.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osedit.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscombobox.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/ostable.php");
 
class osdefaultlistdictionaryedit extends oscomponent {

	private $items = array();
	private $meta;
	private $current = array();
	private $state;// = "isready";
	private $search = array();
	private $cbitemsperpage;
	private $cbpage;
	private $btfirstpage;
	private $btprevpage;
	private $btnextpage;
	private $btlastpage;
	private $lbcountitems;
	private $tbdic;

	function __construct($parent) {
		parent::__construct($parent);
		//$this->meta = $meta;
		//$this->update();
		$this->cbitemsperpage = new oscombobox(0);
		$this->cbitemsperpage->additem(0, "...");
		$this->cbitemsperpage->additem(5, "5");
		$this->cbitemsperpage->additem(10, "10");
		$this->cbitemsperpage->additem(20, "20");
		//$this->cbitemsperpage->additem(50, "50");
		//$this->cbitemsperpage->additem(80, "80");
		//$this->cbitemsperpage->additem(130, "130");
		$this->cbitemsperpage->setevent($this, "onchange", "cbitemsperpageonchange");
		
		$this->cbpage = new oscombobox(0);
		//$this->cbpage->setevent($this, "onchange", "cbpageonchange");
		
		$this->lbcountitems = new oslabel(0);
		
		$this->tbdic = new ostable(0);
		$this->tbdic->setevent($this, "rowonclick", "tbdicrowonclick");
		
		$this->initialize();
	}
	
	function initialize() {
		$count = $this->lbcountitems->value = $this->countitems();
		$this->cbpage->deleteallitems();
		$itemsperpage = $this->cbitemsperpage->selecteditem();
		if ($itemsperpage == 0) {
			//$itemsperpage = 0;
			$this->cbpage->additem(1, "1");
		} else {
			$pages = ceil($count / $itemsperpage);
			for ($page = 1; $page <= $pages; $page++) {
				$this->cbpage->additem($page, "$page");
			}
		}
		if ($this->meta) {
			$this->tbdic->deleteallheaders();
			foreach($this->meta->fields->getall() as $field) {
				$this->tbdic->addheader($field->uuid, $field->name);
			}
			$this->tbdic->deleteallitems();
			$this->getitems();
			foreach($this->items as $item) {
				$this->tbdic->additem($item["id"], $item);
			}
		}
	}
	
	function tbdicrowonclick($name) {
/*
		$this->cbpage->disable();
		$this->update();
*/
	}
	
	function cbitemsperpageonchange() {
		$this->initialize();
		$this->update();
	}
	
	function countitems() {
		if ($this->meta) {
			return $_SESSION["metadata"]->database->countitems($this->meta->uuid);
		}
		//return 780;
	}
	
	function get() {
		$ret = "<div id=".$this->uuid;
		$ret .= $this->visible?"":" hidden";
		$ret .= ">";
		$ret .= $this->lbcountitems->get()." ".$this->cbitemsperpage->get()." ".$this->cbpage->get()."<br>";
		$ret .= $this->tbdic->get();
		//$ret .= "<pre>".print_r($this->items,1)."</pre>";
		$ret .= "Здесь будет таблица ";
		if ($this->meta) {
			$ret .= $this->meta->name;
		}
		$ret .= "</div>";
		return $ret;
	}


	function additem() {
		$this->state = "isadd";
		$this->current = array();
		return true;
		//return $this->current = new ositem( genuuid(), $this->meta);
	}
	
	function deleteitem($mark = true) {
		if ($mark) {
			
		} else {
			// Удалить запись из базы 
			//unset($this->current);
			$this->current = null;
		}
	}

	
	//function searchbyname() {}
	
	//function searchbyuuid() {}
	
	function searchbyfield($field, $value) {
		if ($metafield = $this->meta->fields->byname($field)) {
			$this->search[$metafield->uuid] = $value;
		} elseif ($metafield = $this->meta->fields->byuuid($field)) {
			$this->search[$metafield->uuid] = $value;
		} else {
			return false;
		}
		$this->update();
		return true;
	}
	
	function cancelsearch() {
		$this->search = array();
		$this->update();
	}
	
/*
	function saveitem() {
		return serialize($this->current);
	}
	
	function openitem($data) {
		$this->current = null;
		$this->current = unserialize($data);
	}
*/
/*
	function __get($name) {
		switch ($name) {
			//case "items": { if (!$this->items) { return new ositems($this->meta);} else {return $this->items;} break;} // Это надо выкинуть из метаданных
			//case "items": { return new ositems($this->meta); break;}
			//case "items": { return $this->items; break;}
			case "name": { return $this->meta->name;break;}
			//case "uuid": { return $this->uuid;break;}
		}
	}
*/

/*
	function __set($name, $value) {
		
	}
*/

	function __get($name) {
/*
		if (is_array($this->items)) {
			foreach ($this->meta->fields->getall() as $metafield) {
				if ($name == $metafield->name) {
					return $this->current[$metafield->uuid];
				}
			}
		}
*/
		
/*
		foreach ($this->meta->tabulars->getall() as $metatabular) {
			if ($name == $metatabular->name) {
				return $this->tabulars[$metatabular->uuid];
			}
		}
*/
		
/*
		switch ($name) {
			case "uuid": {return $this->meta->uuid; break;}
			case "meta": {return $this->meta; break;}
		}
*/
	}
	
	function __set($name, $value) {
		//foreach ($this->fields as $field) {
		//	${$field->meta->name} = $field;
		//}
		//eval($sender->meta->beforechange);
		switch ($name) {
			case "meta": { 
				$this->meta = $value;
				$this->initialize();
				//$this->update();
				break;
			}
		}
		if ($this->meta) {
			foreach ($this->meta->fields->getall() as $metafield) {
				if ($name == $metafield->name) {
					eval($metafield->beforechange);
					$this->current[$metafield->uuid] = $value;
					eval($metafield->afterchange);
					return true;
				} 
			}	
		}
	}	
	
	function count() {
		if (!is_array($this->items)) {
			return 0;
		} else {
			return count($this->items);
		}
	}
	
	function first() {
		if ($this->current()) { 
			reset($this->items);
		}
	}
	
	function next() {
		if ($this->current()) { 
			if ($item = next($this->items)) return $this->current = $item; else return $item;
		}
	}
	
	function current() {
		if (is_array($this->items)) {
			return $this->current = current($this->items);
		} else return false;
	}
	
	function prev() {
		if ($this->current()) { 
			if ($item = prev($this->items)) return $this->current = $item; else return $item;
		}
	}
	
	private function getitems() {
		$sql = "SELECT * FROM ".$this->meta->uuid;
/*
		foreach ($this->search as $key=>$value) {
			$sql .= " WHERE $key = '".$value."'";
		}
*/
		$sql .= ";";
		$this->items = $_SESSION["metadata"]->database->select($sql);
		//$this->current();
	}
	
	function save() {
		if ($this->state == "isedit") {
			$action = "UPDATE ".$this->meta->uuid." SET";
			foreach ($this->meta->fields->getall() as $metafield) {
				$action .= " ". $metafield->uuid ." = '".addslashes($this->current[$metafield->uuid])."',";
			}
			$action = substr_replace($action, "", -1, 1);
			$action .= " WHERE (id = '".$this->current["id"]."')";
			$action .= $this->owner ? " AND (owner = '".$this->owner."')" : "";
			$action .= ";";
			$_SESSION["metadata"]->database->query($action);
		} elseif ($this->state == "isadd") {
			$action = "INSERT INTO ".$this->meta->uuid."(";
			$action .= " id, ";
			foreach ($this->meta->fields->getall() as $metafield) {
				$action .=  " ". $metafield->uuid.",";
			}
			$action = substr_replace($action, ")", -1, 1);
			$action .= " VALUES('";
			$action .= genUUID()."',";
			foreach ($this->meta->fields->getall() as $metafield) {
				$action .= " '".addslashes($this->current[$metafield->uuid])."',";
			}
			$action = substr_replace($action, ");", -1, 1);
			//echo $action;
			$_SESSION["metadata"]->database->query($action);
		} else {}
		//echo $action;
		$this->state = "isready";
		$this->update();
	}
	
	function cancel() {
		
	}
	
	/*
	private function get() {
		//unset($this->item);
		$sql = "SELECT * FROM ".$this->object->uuid;
		$sql .= $this->owner ? " WHERE owner = '".$this->owner."'" : "";
		$sql .= " ORDER BY ".$this->object->sortby." ".$this->object->direction;
		$sql .= " LIMIT ".$this->object->limit." OFFSET ".$this->object->offset;
		$sql .= ";";
		echo $sql;
		$result = $_SESSION["metadata"]->database->select($sql);
		//echo print_r($result,1);

		foreach ($result as $item) {
			$uuid = $item["id"];
			$this->item[$uuid] = new ositem($this->object, $uuid, $item);
		}

		//return $this->item;
	}
*/
	
/*
	function getallitems() {
		$items = array();
		foreach () {
			$item = new ositem( genuuid(), $this->meta);
		}
	}
/*	
	function __invoke() {
		return $this->getallitems();
	}
*/
}
?>
